﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeetCode
{
   public  class _28ImplementstrStr :IRun
    {
        public void Run()
        {
            StrStr("abbcd","bc");
        }
        public int StrStr(string haystack, string needle)
        {
            if (needle.Length == 0)
                return 0;
            if (needle.Length > haystack.Length)
                return -1;
            int j = 0;
            int i = 0;
            for (i = 0; i < haystack.Length; i++)
            {
                if (j == needle.Length)
                {
                    return i - needle.Length;
                }
                if (haystack[i] == needle[j])
                {
                    j++;
                }
                else
                {
                    i -= j;
                    j = 0;
                }
            }
            if (j == needle.Length)
            {//判断完成
                return i - needle.Length;
            }
            return -1;
        }
    }
}
